﻿Imports System.ServiceModel

Public Class frmAltaEmisor
    Private _random As New Random

    Private Sub btnAlta_Click(sender As System.Object, e As System.EventArgs) Handles btnAlta.Click
        Dim transactionId As Long = _random.[Next]()
        Dim transactionIdOriginal As Long = 0
        Dim token As String = String.Empty
        Dim Errores As String = String.Empty
        Using Cliente = New SrvClients.ClientesClient()
            Dim Util As Utilerias = New Utilerias()
            Errores = Util.CreateTokenAlta(txtRFCIntegrador.Text, transactionId, token)
            If (String.IsNullOrEmpty(Errores) = False) Then
                ShowUnknownError(Errores)
                Exit Sub
            End If
            Try
                Dim Emisor As SrvClients.AltaEmisor = New SrvClients.AltaEmisor()
                Emisor.RFC = txtRFCEmisor.Text
                Emisor.RazonSocial = txtRazonSocial.Text
                Emisor.CorreoElectronico = txtCorreoElectronico.Text
                Dim respuesta = Cliente.Registrar(Emisor, txtRFCIntegrador.Text, token, transactionId, transactionIdOriginal)
                txtRFCEmisorR.Text = respuesta.RFC
                txtEstatus.Text = respuesta.Estatus
                txtClaveCertificado.Text = respuesta.ClaveCertificado
            Catch validatioFault As FaultException(Of SrvClients.FallaValidacion)
                Using errorForm = New frmError(TranslateFault.ToValidationError(validatioFault.Detail))
                    errorForm.ShowDialog()
                End Using
            Catch serviceFault As FaultException(Of SrvClients.FallaServicio)
                Using errorForm = New frmError(TranslateFault.ToServiceError(serviceFault.Detail))
                    errorForm.ShowDialog()
                End Using
            Catch sessionFauld As FaultException(Of SrvClients.FallaSesion)
                Using errorForm = New frmError(TranslateFault.ToSessionError(sessionFauld.Detail))
                    errorForm.ShowDialog()
                End Using
            Catch ex As Exception
                ShowUnknownError(ex.Message)
            End Try
        End Using
    End Sub

    Private Sub ShowSessionError(ByVal sessionError As Entity.SessionError)
        MessageBox.Show(String.Format("Status {0}: {1}", sessionError.Status.ToString(), sessionError.Description), "Ecodex UniTest")
    End Sub

    Private Sub ShowUnknownError(ByVal p1 As String)
        MessageBox.Show(String.Format("Error: {0}", p1), "Ecodex UniTest")
    End Sub

    Private Sub ShowResult(ByVal p1 As String)
        MessageBox.Show(p1, "Ecodex UniTest")
    End Sub

 
End Class